package cn.ffcs.uec.insurance.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.insurance.action.bean.UecPeriodstateEditBean;
import cn.ffcs.uec.insurance.model.UecBmclass;
import cn.ffcs.uec.insurance.model.UecPeriod;
import cn.ffcs.uec.insurance.model.UecPeriodstate;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecPeriodstateEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -6096343177536382090L;

	private UecPeriodstateEditBean bean = new UecPeriodstateEditBean();

	private String opType;


	@Resource
	private IdentityCardConfigManager identityCardConfigManager;

	private UecPeriodstate uecPeriodstate;
	
	private UecBmclass uecBmclass;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
		
		
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecPeriodstateEditWin() throws Exception {
		bindCombobox();
		opType = (String) arg.get("opType");
		uecBmclass = (UecBmclass) arg.get("uecBmclass");
		if (opType.equals("add")) {
			bean.getUecPeriodstateEditWin().setTitle("新增");
			
		} else if (opType.equals("edit")) {
			strToDate();
			bean.getUecPeriodstateEditWin().setTitle("修改");
			strToDate();
		} else if (opType.equals("view")) {
			strToDate();
			bean.getUecPeriodstateEditWin().setTitle("查看");
			strToDate();
			PubUtil.disableComponent(bean.getUecPeriodstateEditWin());
		} 
	}
	

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffWorkData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
		uecPeriodstate = (UecPeriodstate) arg.get("uecPeriodstate");
		UecPeriod uecPeriod = null;
		UecPeriodstate uecPeriodstateObj = UecPeriodstate.newInstance();
		if (uecPeriodstate != null) {
			PubUtil.fillPoFromEditorBean(uecPeriodstate, bean);
			uecPeriod = uecPeriodstate.getUecPeriod();
		} else {
			uecPeriodstate = uecPeriodstateObj;
			uecPeriod = uecPeriodstate.getUecPeriod();
			if (uecPeriod != null) {
				PubUtil.fillPoFromEditorBean(uecPeriod, bean);
			} else {
				uecPeriod = UecPeriod.newInstance();
				PubUtil.fillPoFromEditorBean(uecPeriod, bean);
			}
			uecPeriodstate.setUecBmclassId(uecBmclass.getId());
			PubUtil.fillPoFromEditorBean(uecPeriodstate, bean);
		}
		
		if (opType.equals("add")) {
			
			String daccountdate = DateUtil.dateToStr(this.bean.getDaccountdate().getValue());
			uecPeriodstate.setDaccountdate(daccountdate);
			String cstartdate = DateUtil.dateToStr(this.bean.getCstartdate().getValue());
			uecPeriod.setCstartdate(cstartdate);
			String cenddate = DateUtil.dateToStr(this.bean.getCenddate().getValue());
			uecPeriod.setCenddate(cenddate);
			uecPeriodstate.setStatusCd("1000");
			uecPeriod.addOnly();
			uecPeriodstate.setUecPeriodId(uecPeriod.getId());
			uecPeriodstate.addOnly();
			Events.postEvent("onOK", this.self, uecPeriodstate);
			onCancel();
			Messagebox.show("新增成功！");
		} else if (opType.equals("edit")) {
			String daccountdate = DateUtil.dateToStr(this.bean.getDaccountdate().getValue());
			uecPeriodstate.setDaccountdate(daccountdate);
			String cstartdate = DateUtil.dateToStr(this.bean.getCstartdate().getValue());
			uecPeriod.setCstartdate(cstartdate);
			String cenddate = DateUtil.dateToStr(this.bean.getCenddate().getValue());
			uecPeriod.setCenddate(cenddate);
			uecPeriodstate.setStatusCd("1000");
			uecPeriod.updateOnly();
			uecPeriodstate.updateOnly();
			Events.postEvent("onOK", this.self, uecPeriodstate);
			onCancel();
			Messagebox.show("修改成功！");
		}
		
		onCancel();
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecPeriodstateEditWin().onClose();
	}

	private String checkUacStaffWorkData() {
		// BaseInfo Check
		/*if (StrUtil.isNullOrEmpty(bean.getJobName().getValue())) {
			return "请填写岗位名称";
		} else if (StrUtil.checkBlank(bean.getJobName().getValue())) {
			return "岗位名称中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) {
			return "请填写在本单位从事外包开始日期";
		} */

		// IDCARD Real Name Check
		/*if (EnumUacStaffInfo.CERT_TYPE_IDCARD.getValue().equals(
				bean.getCertType().getSelectedItem().getValue())) {
			String idNum = bean.getCertNumber().getValue();
			String idName = bean.getCertName().getValue();
			try {
				boolean isRealName = CertUtil.checkIdCard(idNum, idName);

				if (!isRealName) {
					return "实名认证未通过";
				}
			} catch (Exception e) {
				return "调用国政通接口失败";
			}

		}*/

		return null;
	}
	/**
	 * 字符串转date
	 * @throws ParseException 
	 * @throws WrongValueException 
	 */
	private void strToDate() throws WrongValueException, ParseException {
		if (arg.get("uecPeriodstate") != null) {
			uecPeriodstate = (UecPeriodstate) arg.get("uecPeriodstate");
			PubUtil.fillBeanFromPo(uecPeriodstate, bean);
			PubUtil.fillBeanFromPo(uecPeriodstate.getUecPeriod(), bean);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (null != uecPeriodstate.getDaccountdate()) {
				bean.getDaccountdate()
						.setValue(sdf.parse(uecPeriodstate.getDaccountdate()));
			}
		}
	}
	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
		String[][] isSend = {{"Y", "是"}, {"N", "否"}};
		for (int i = 0; i < isSend.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(isSend[i][j]);
				nodeVo.setName(isSend[i][j + 1]);
			}
			nodeVoIsSendList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getAccountmark(),nodeVoIsSendList);
		ListboxUtils.rendererForEdit(bean.getRecacuflag(),nodeVoIsSendList);
		ListboxUtils.rendererForEdit(bean.getCheckflag(),nodeVoIsSendList);
		ListboxUtils.rendererForEdit(bean.getEnableflag(),nodeVoIsSendList);
		ListboxUtils.rendererForEdit(bean.getIsapproved(),nodeVoIsSendList);
	}
}
